package com.ald.crossex.service;

import com.ald.crossex.dao.CeCoreSystemDao;
import com.ald.crossex.dao.CeGoodsDao;
import com.ald.crossex.dao.CeGoodsTypeDao;
import com.ald.crossex.dto.JqGridRequest;
import com.ald.crossex.dto.SendOrderDto;
import com.ald.crossex.entity.TCeCoreSystem;
import com.ald.crossex.entity.TCeGoodsType;
import com.ald.crossex.entity.VSendOrder;
import com.ald.crossex.system.SystemCache;
import com.ald.crossex.system.SystemCacheItem;
import com.ald.crossex.util.JqGridFilterUtils;
import com.ald.crossex.util.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lishijiong on 15/2/5.
 */
@Service
public class SystemService {
    @Autowired
    private CeCoreSystemDao systemDao;
    @Autowired
    private HibernateTemplate hibernateTemplate;


    @Transactional
    public void addSystem(TCeCoreSystem system) {
        system.setUpdateTime(new Date());
        systemDao.create(system);
    }

    @Transactional
    public void updateSystem(TCeCoreSystem system) {
        TCeCoreSystem s = systemDao.query(system.getSid());
        s.setUpdateTime(new Date());
        s.setRemark(system.getRemark());
        s.setDataType(system.getDataType());
        s.setIsValid(system.getIsValid());
        s.setValue(system.getValue());
        //刷新缓存
        SystemCache.put(s.getKey(), s);
        //保存变量到数据库
        systemDao.update(s);

    }

    @Transactional
    public int delSystem(String sid){
        String[] ids = sid.split(",");
        int i=0;
        for(;i<ids.length;i++){
            hibernateTemplate.bulkUpdate("update TCeCoreSystem set isValid = 0 where sid = ?",new Object[]{Integer.parseInt(ids[i])});
        }
        return i;

    }

    /**
     * 获取所有的系统参数
     *
     * @return
     */
    public List<TCeCoreSystem> listSystem() {
        return systemDao.queryByHql("from TCeCoreSystem where isValid = 1 order by sid asc", new Object[]{});
    }

    /**
     * 分页获取系统参数
     * @param request
     * @return
     */
    public Page listSystem(JqGridRequest request){
        Page page = new Page(request.getPage(), request.getRows());

        String conditionSql = " where 1=1  and o.isValid = 1 ";

        if (StringUtils.isNotEmpty(request.getFilters())) {
            conditionSql += JqGridFilterUtils.convertJqGridFilter2String(request.getoFilters());
        }

        String orderSql = " order by " + (StringUtils.isEmpty(request.getSidx()) ? "sid" : request.getSidx()) + " " + request.getSord();

        List<TCeCoreSystem> list = systemDao.queryPageByHQL("select  count(*) from TCeCoreSystem o " + conditionSql, "from TCeCoreSystem o" + conditionSql + orderSql, new Object[]{}, page);

        page.setDatas(list);

        return page;
    }

    /**
     * 根据key获取系统参数
     * @param key
     * @return
     */
    public TCeCoreSystem getSystemByKey(String key){


        SystemCacheItem item = SystemCache.get(key);
        if(item!=null){
            return (TCeCoreSystem)item.getObject();
        }

        List<TCeCoreSystem> list = hibernateTemplate.find("from TCeCoreSystem where key = "+key);
        if(list.size()>0){
            TCeCoreSystem system  = list.get(0);
            SystemCache.put(system.getKey(),system);
            return system;
        }
        return null;
    }

    /**
     * 初始化缓存系统参数
     */
    public int cacheSystem(){
        List<TCeCoreSystem> list = listSystem();
        int i=0;
        for (; i < list.size(); i++) {
            TCeCoreSystem system = list.get(i);
            SystemCache.put(system.getKey(), system);
        }
        return i;
    }

}
